﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Globalization;

namespace WindowsFormsApplication1
{
    public class Patient
    {
        public int? id;
        public string patName { get; set; }
        public string surname { get; set; }
        public string patronymic { get; set; }
        public DateTime dob { get; set; }
        public List<int> patDiagnose { get; set; }

        public Patient()
        {
            dob = DateTime.Today;

        }

        public string ToDbFormat()
        {
            string ds = "";
           
                for (int i = 0; i < patDiagnose.Count; i++)
                {
                    if (i != patDiagnose.Count - 1)
                    {
                        ds += patDiagnose[i].ToString() + ";";
                    }
                    else
                    {
                        ds += patDiagnose[i].ToString();
                    }
                }
            
            return id.Value + "," + patName + "," + surname + "," + patronymic + "," + dob.ToString("dd-MM-yyyy") + "," + ds;
        }


        public static Patient parseFromDb(String s)
        {
            string[] ar = s.Split(',');
            return parseFromStringArray(ar);
        }

        public static Patient parseFromStringArray(string[] ar)
        {
            Patient p = new Patient();
            if (ar[0] != null)
            {
                p.id = Convert.ToInt32(ar[0]);
            }
            p.patName = ar[1];
            p.surname = ar[2];
            p.patronymic = ar[3];
            List<int> diags = new List<int>();
            if (ar[5] =="")
            {
                diags.Add(0);
            }
            else
            {

                foreach (string el in ar[5].Split(';'))
                {
                    diags.Add(Convert.ToInt32(el));
                }

            }
            p.patDiagnose = diags;
            DateTime dt;
            if (DateTime.TryParseExact(ar[4], "dd-MM-yyyy", null, DateTimeStyles.None, out dt))
            {
                p.dob = dt;
            }

            return p;
        }
    }

}